package thread_test;

public class ThreadDemo9 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            // isInterrupted() 方法相当于标志位 isQuit
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("hello thread!!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
            System.out.println("线程 t 工作完毕");
        });
        t.start();
        // 让 t 线程执行三秒后结束
        Thread.sleep(3000);
        // 相当于设置 isQuit 为 true
        t.interrupt();
        System.out.println("让 t 线程结束");
    }
}
